<?php

namespace App\CLI;

use Forxx\console\CLIRunner;
use Forxx\console\CConsole;

class CGenerate extends CLIRunner
{
    CONST TYPE_PAGE = 1;
    CONST TYPE_SERVICE = 2;
    CONST TYPE_API = 3;
    CONST TYPE_DAO = 4;
    
    public function page_action($name = null)
    {
        if(empty($name))
        {
            $name = CConsole::prompt('input table name : ');
        }
        self::generate_page_file($name);
    }
    
    public function service_action($name = null)
    {
        if(empty($name))
        {
            $name = CConsole::prompt('input table name : ');
        }
        self::generate_service_file($name);
    }
    
    public function api_action($name = null)
    {
        if(empty($name))
        {
            $name = CConsole::prompt('input table name : ');
        }
        self::generate_api_file($name);
    }
    
    public function dao_action($name=null)
    {
        if(empty($name))
        {
            $name = CConsole::prompt('input table name : ');
        }
        self::generate_dao_file($name);
    }
    
    public function all_action($name = null)
    {
        if(empty($name))
        {
            $name = CConsole::prompt('input table name : ');
        }
        self::generate_page_file($name);
        self::generate_service_file($name);
        self::generate_api_file($name);
        self::generate_dao_file($name);
    }
    public static function generate_page_file($name,$tpl_file_path = NULL,$output_path = NULL)
    {
        self::generate_file($name,self::TYPE_PAGE,$tpl_file_path,$output_path);
    } 

    public static function generate_service_file($name,$tpl_file_path = NULL,$output_path = NULL)
    {
        self::generate_file($name,self::TYPE_SERVICE,$tpl_file_path,$output_path);
    }

    
    public static function generate_api_file($name,$tpl_file_path = NULL,$output_path = NULL)
    {
        self::generate_file($name,self::TYPE_API,$tpl_file_path,$output_path);
    }

    
    public static function generate_dao_file($name,$tpl_file_path = NULL,$output_path = NULL)
    {
        self::generate_file($name,self::TYPE_DAO,$tpl_file_path,$output_path);
    } 
    

    protected static function generate_file($name,$type=self::TYPE_PAGE,$tpl_file_path = NULL,$output_path = NULL)
    {
        $conf = self::get_generate_config($name,$type);       
        $generateFileName = L_APP_PATH.$conf['path'].DIRECTORY_SEPARATOR.$conf['file'].".php";
        if(empty($tpl_file_path))
        {
            $tpl_file_path = self::get_tpl_file($type);
        }
        if(empty($output_path))
        {
            $output_path = $generateFileName;
        }
        $repalce_keys = [
        'TestPage'=>$conf['file'],
        'test_service'=>$conf['file'],
        'TestTable'=>$conf['file'],
        'test'=>$conf['lower'],
        'Test'=>$conf['upper'],
        ];
        $content = self::get_generate_content($tpl_file_path,$repalce_keys);
        self::output_file($output_path, $content);
    }

    protected static function get_generate_config($name,$type = self::TYPE_PAGE)
    {
        $res = [];
        $res['lower'] = strtolower($name);
        $res['upper'] = str_replace(' ','',ucwords(str_replace('_', ' ', $name)));
        $filename = $name;
        $path = 'Action';
        switch ($type)
        {
            case self::TYPE_PAGE:
                $filename = $res['upper'].'Page';
                $path = 'Action\Page';
                break;
            case self::TYPE_SERVICE:
                $filename = $res['lower'].'_service';
                $path = 'Action\Service';
                break;
            case self::TYPE_API:
                $filename = $res['upper'];
                $path = 'Api';
                break;
            case self::TYPE_DAO:
                $filename = $res['upper'].'Table';
                $path = 'Dao';
                break;
        }
        $res['file'] = $filename;
        $res['path'] = $path;
        return $res;
    }
    
    
    public static function get_generate_content($tpl_file_path,$replace_keys =[])
    {
        if(!file_exists($tpl_file_path))
        {
            return '';
        }
        $content = file_get_contents($tpl_file_path);
        foreach ($replace_keys as $from => $to)
        {
            $content = str_replace($from, $to, $content);
        }
        return $content;
    }
    
    protected static function get_tpl_file($key)
    {
        $files = self::get_tpl_files();
        if(isset($files[$key]))
        {
            return $files[$key];
        }
        CConsole::print_line("can't find the '{$key}' template file!");
        die();
    }
    
    protected static function get_tpl_files()
    {
        return [
            self::TYPE_PAGE => L_FRAMEWORK_PATH.'default'.DIRECTORY_SEPARATOR.'Action'.DIRECTORY_SEPARATOR.'Page'.DIRECTORY_SEPARATOR.'test_page.php',
            self::TYPE_SERVICE => L_FRAMEWORK_PATH.'default'.DIRECTORY_SEPARATOR.'Action'.DIRECTORY_SEPARATOR.'Service'.DIRECTORY_SEPARATOR.'test_service.php',
            self::TYPE_API => L_FRAMEWORK_PATH.'default'.DIRECTORY_SEPARATOR.'Api'.DIRECTORY_SEPARATOR.'Test.php',
            self::TYPE_DAO => L_FRAMEWORK_PATH.'default'.DIRECTORY_SEPARATOR.'Dao'.DIRECTORY_SEPARATOR.'TestTable.php',
        ];
    }
    
    protected static function check_file($file_path)
    {
        if(file_exists($file_path))
        {
            return false;
        }
        return true;
    }
    
    protected static function output_file($file,$content)
    {
        if(!self::check_file($file))
        {
            $is_over = CConsole::confirm("the file {$file} is Already Exists! overwrite it?",false);
            if(! $is_over)
            {
                return;
            }
        }
        file_put_contents($file, $content);
    }

}